package com.restart.game.common.util.img;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Random;

/**
 * @author ReStartLin
 * @data 2018/12/26 16:53
 * @classDesc: 功能描述:
 */
public class ImgOSS {
    private static ImgOSS imgOSS;

    private ImgOSS() {
    }

    private String ESSDOMAIN = "http://restartimg.oss-cn-zhangjiakou.aliyuncs.com";
    private String stylename = "?x-oss-process=style/do";
    private String stylenameJIE = "?x-oss-process=style/jie";
    private String endpoint = "http://oss-cn-zhangjiakou.aliyuncs.com";
    private String accessKeyId = "LTAI2n5VdVBMsVHF";
    private String accessKeySecret = "MkCYXl34VOxFkC4M4WLJmuzLAjU8Y8";
    private String bucketName = "restartimg";

    public static ImgOSS getInstance() {
        if (imgOSS == null) {
            imgOSS = new ImgOSS();
        }
        return imgOSS;
    }

    public String uploadFile(String url) throws IOException {
        int i = url.lastIndexOf("/");
        if (i == url.length() - 1) {
            url = url.substring(0, i);
        }
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        ossClient.createBucket(bucketName);
        String suffix = ".png";
        int suffixIndex = url.lastIndexOf(".");
        if (suffixIndex > i) {
            suffix = url.substring(suffixIndex, url.length());
        }
        //临时存储图片
        String s = imageName();
        if (suffix.equals(".gif")) {
            ossClient.shutdown();
            return "";
        }
        String imgName = s + suffix;
        File imgFile = File.createTempFile(s, suffix);
        download(imgFile, url);//下载
        //处理打码
        File newImgFile = ImgMosaic.mosaic(imgFile, "png");
        InputStream content = new FileInputStream(newImgFile);
        ObjectMetadata meta = new ObjectMetadata(); // 创建上传Object的Metadata
        meta.setContentLength(newImgFile.length());   // 必须设置ContentLength
        ossClient.putObject(bucketName, imgName, content);

        //关闭
        imgFile.deleteOnExit();
        newImgFile.deleteOnExit();
        ossClient.shutdown();
        return ESSDOMAIN + "/" + imgName;

    }





    private void download(File imgFile, String path) throws IOException {
        // 构造URL
        URL url = new URL(path);
        // 打开连接
        URLConnection con = url.openConnection();
        //设置请求超时为5s
        con.setConnectTimeout(5 * 1000);
        // 输入流
        InputStream is = con.getInputStream();
        // 1K的数据缓冲
        byte[] bs = new byte[1024];
        // 读取到的数据长度
        int len;
        // 输出的文件流
        OutputStream os = new FileOutputStream(imgFile);
        // 开始读取
        while ((len = is.read(bs)) != -1) {
            os.write(bs, 0, len);
        }
        os.close();
        is.close();

    }

    public static String imageName() {
        Random random = new Random();//生成随机数
        String strDate = Long.toString(System.currentTimeMillis());
        for (int i = 0; i < 3; i++) {
            strDate = strDate + random.nextInt(9);
        }
        return strDate;
    }
}
